package com.jack.lib.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ReflectUtils
import com.jack.lib.core.ext.arguments
import com.jack.lib.core.ext.toastW
import com.jack.lib.core.net.JBaseData
import com.jack.lib.core.net.JNetView
import com.jack.lib.core.ui.JWindow

/**
 * author : jack(黄冲)
 * e-mail : 907755845@qq.com
 * create : 2019-10-12
 * desc   :
 */

abstract class BaseFragment<VB: ViewBinding> : Fragment(), JNetView{

    var isInitialize = false
    protected lateinit var binding: VB


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = ReflectUtils.reflect(arguments(0)).method("inflate", inflater, container, false).get()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isInitialize = true
        initLayout()
    }

    abstract fun initLayout()


    override fun showLoading() {
        JWindow.showLoading(requireActivity() as AppCompatActivity)
    }

    override fun hideLoading() {
        JWindow.hideLoading()
    }

    override fun onFill(bean: JBaseData<*>) {
        toastW(bean.message())
    }

    override fun onError(cause: Throwable) {
        toastW(cause.message)
    }

    override fun lifecycle(): Lifecycle = lifecycle

}